package com.cckat.controller;

import com.cckat.base.BaseController;
import com.cckat.en.HouseStatus;
import com.cckat.entity.*;
import com.cckat.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author 静海基地
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private DictService dictService;

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private HouseUserService houseUserService;

    @DubboReference
    private HouseBrokerService houseBrokerService;


    @RequestMapping
    public String findPage(Model model, @RequestParam Map<String, Object> filters) {
        //查询房屋初始化信息存储到model中
        saveAllDictToRequestScope(model);
        //获取分页信息
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("filters", filters);
        model.addAttribute("page", page);
        return "house/index";
    }


    @RequestMapping("/create")
    public String create(Model model) {
        //查询房屋初始化信息存储到model中
        saveAllDictToRequestScope(model);
        return "house/create";
    }


    void saveAllDictToRequestScope(Model model) {
        //获取所有小区的所有信息
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList", communityList);
        model.addAttribute("houseTypeList", dictService.findDictListByParentDictCode("houseType"));
        model.addAttribute("floorList", dictService.findDictListByParentDictCode("floor"));
        model.addAttribute("buildStructureList", dictService.findDictListByParentDictCode("buildStructure"));
        model.addAttribute("decorationList", dictService.findDictListByParentDictCode("decoration"));
        model.addAttribute("directionList", dictService.findDictListByParentDictCode("direction"));
        model.addAttribute("houseUseList", dictService.findDictListByParentDictCode("houseUse"));
    }


    @RequestMapping("/save")
    public String save(House house, Model model) {
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        houseService.insert(house);
        return successPage("添加房源信息成功", model);
    }


    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        saveAllDictToRequestScope(model);
        return "house/edit";
    }


    @RequestMapping("/update")
    public String update(House house, Model model) {
        houseService.update(house);
        return successPage("修改房源信息成功", model);
    }


    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id, Model model) {
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        houseService.publish(id, status);
        return "redirect:/house";
    }

    @RequestMapping("/{id}")
    public String show(@PathVariable("id") Long id, Model model){
        //获取房源信息
        House house = houseService.getById(id);
        model.addAttribute("house", house);

        //获取小区信息
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community", community);

        //获取房源图片信息
        List<HouseImage> houseImageList1 = houseImageService.findHouseImageListByHouseIdAndType(id, 1);
        model.addAttribute("houseImageList1", houseImageList1);

        //获取房产图片信息
        List<HouseImage> houseImageList2 = houseImageService.findHouseImageListByHouseIdAndType(id, 2);
        model.addAttribute("houseImageList2", houseImageList2);

        //获取房产经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        model.addAttribute("houseBrokerList", houseBrokerList);

        //获取房东信息
        List<HouseUser> houseUserList = houseUserService.findHouseUserListByHouseId(id);
        model.addAttribute("houseUserList", houseUserList);
        return "house/show";
    }
}
